##########################
####### Figure A1 ########
##########################

library(readxl)
library(dplyr)
library(tidyr)
library(ggplot2)
library(sf)
library(maps)
library(GADMTools)
library(tmap)
library(maptools)
library(stringi)
library(readr) 

wd <- setwd("Your_Directory/JCR_Jooetal_replication/Figure A1")

##### Map for India
IndiaRes <- read.csv("IndiaRespondents.csv")

#Download shapefile
India_gadm1 <- gadm_sp_loadCountries(fileNames = 'IND', level = 1, basefile = './shapefiles', simplify=0.01)
IndiaShape1<-India_gadm1[[2]]
IndiaShape.sf1 <- st_as_sf(IndiaShape1)

#Merge with respondents data
IndiaShape.sf11<-merge(IndiaShape.sf1,IndiaRes,by=c('NAME_1'),all.x=TRUE)

#Generate Map
tm_shape(IndiaShape.sf11)+ tm_polygons (legend.is.portrait = FALSE,"Respondents",style="cont",breaks=c(0,20,50,100,150),
                                        palette=c("grey99","grey80","grey50","grey30"),
                                        colorNA = NULL, legend.show = TRUE)+
  tm_layout(frame = FALSE, legend.outside = TRUE, 
            legend.outside.position = "bottom",main.title.size=20,
            legend.outside.size = .25, 
            legend.text.size = 3,
            legend.title.size=6,
            legend.format = list(fun = function(x) {
              ifelse(x %in% c(0,150), x, "")
            }))

##### Map for Japan

#Import region and prefecture information
prefectures_regions <- read_excel("prefectures_regions.xlsx")
prefectures_regions$NAME_1<-stri_trans_general(str = prefectures_regions$Prefecture, 
                                               id = "Latin-ASCII")

JapanRes <- read.csv("JapanRespondents.csv")

#Download Shapefile and merge data
Japan_gadm1 <- gadm_sp_loadCountries(fileNames = 'JPN', level = 1, basefile = './shapefiles', simplify=0.01)
Japan_gadm1a<-Japan_gadm1[[2]]
Japan_gadm1a$NAME_1<-stri_trans_general(str = Japan_gadm1a$NAME_1, 
                                        id = "Latin-ASCII")
Japan_gadm1a$NAME_1[Japan_gadm1a$NAME_1=="Naoasaki"]<-"Nagasaki"
Japansf.sf1 <- st_as_sf(Japan_gadm1a)
Japansf.sf1<-merge(Japansf.sf1,prefectures_regions,by="NAME_1",all.x=TRUE)

#Generate Map
nc2 <- Japansf.sf1 %>% 
  group_by(Region) %>% st_make_valid() %>% 
  summarise(geometry = st_union(geometry))  %>% nngeo::st_remove_holes()
nc3<-merge(nc2,prefectures_regions,by="Region",all.x=TRUE)
nc3<-merge(nc3,JapanRes,by="Region",all.x=TRUE)

 tm_shape(nc3)+ tm_polygons ("Respondents",legend.is.portrait = FALSE,style="cont",breaks=c(0,50,150,200,350),
                                          palette=c("grey99","grey87","grey50","grey30"),
                                          colorNA = "white", legend.show = TRUE)+
   tm_layout(frame = FALSE, legend.outside = TRUE, 
             legend.outside.position = "bottom",main.title.size=20,
             legend.outside.size = .2, 
             legend.text.size = 3,
             legend.title.size=6,
             legend.format = list(fun = function(x) {
               ifelse(x %in% c(0,350), x, "")
             }))

